package com.maikang.exammall.entity.dto.order;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.maikang.exammall.entity.Order;
import com.maikang.exammall.entity.constant.OrderStatus;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Calendar;
import java.util.Date;

@Data
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderBrief {
    private Long userId;
    private Long orderId;
    private Long hospitalId;
    private String outTradeNo;
    private Integer status;
    private String hospitalName;
    private String hospitalAvatarUrl;
    private Long amount;
    @ApiModelProperty("过期的时间戳， 毫秒为单位")
    private Date timeExpire;
    // entity的话才不需要写 timezone
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastModifiedDate;
    private String mobile;
    private String remark;

    public OrderBrief(Order order) {
        userId = order.getUserId();
        orderId = order.getId();
        outTradeNo = order.getOutTradeNo();
        status = order.getStatus();
        hospitalName = order.getOrderItems().get(0).getHospitalName();
        hospitalAvatarUrl = order.getOrderItems().get(0).getHospitalAvatarUrl();
        hospitalId = order.getOrderItems().get(0).getHospitalId();
        amount = order.getAmount();
        mobile = order.getMobile();
        remark = order.getRemark();
        Date timeExpire = null;
        if (status.equals(OrderStatus.TO_PAY)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(order.getCreatedDate());
            calendar.add(Calendar.HOUR, 2); // 订单
            timeExpire = calendar.getTime();
        }
        this.timeExpire = timeExpire;
        this.createdDate = order.getCreatedDate();
        this.lastModifiedDate = order.getLastModifiedDate();
    }
}